package com.chenl.provider.DEMOS.WebService.参考;

import lombok.extern.slf4j.Slf4j;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;

import javax.xml.namespace.QName;

/**
 * @author : tianzhengwei
 * @date : 2021/12/13
 * @description :
 */
@Slf4j
public class GetWSByAxis2 {
    private static String EndPointUrl;
    private static String QUrl="http://webservice.business.wiseda.com/";
    private QName opAddEntry;
    public String WSUrl="";
    public RPCServiceClient setOption() throws AxisFault
    {
        RPCServiceClient serviceClient = new RPCServiceClient();
        Options options = serviceClient.getOptions();
        EndpointReference targetEPR = new EndpointReference(WSUrl);
        options.setTo(targetEPR);
//        //确定调用方法
//        options.setAction("urn:"+methodName);
//        options.setProperty(HTTPConstants.CHUNKED, "false");// 把chunk关掉后，会自动加上Content-Length
//        //解决高并发链接超时问题
//        options.setManageSession(true);
//        options.setProperty(HTTPConstants.REUSE_HTTP_CLIENT,true);
//
//        //设置响应超时，默认5s
//        options.setProperty(HTTPConstants.SO_TIMEOUT, TIMEOUT_MILLISECONDS);
//        //设置连接超时，默认5s
//        options.setProperty(HTTPConstants.CONNECTION_TIMEOUT, TIMEOUT_MILLISECONDS);
        return serviceClient;
    }

    public QName getQname(String Option){

        return new QName (QUrl,Option);
    }
    //返回String
    public Object getStr(String Option, Object[] params)
    {
        Object str;
        // 使用RPC方式调用WebService
        RPCServiceClient serviceClient = null;
        try{
            serviceClient =this.setOption();

            opAddEntry =this.getQname(Option);

            str = (Object) serviceClient.invokeBlocking(opAddEntry,
                    params, new Class[]{String.class })[0];
        } catch (org.apache.axis2.AxisFault e) {
            log.error("第三方接口异常",e);
            if(e.getCause()!=null&&e.getCause().toString().toLowerCase().contains("timeout")){
                throw new RuntimeException("第三方服务连接超时，请稍候重试！");
            }
            throw new RuntimeException("第三方服务异常，请稍候重试！");
        } finally {
            try {
                if(serviceClient != null){
                    serviceClient.cleanupTransport();
                }
            } catch (org.apache.axis2.AxisFault e) {
                log.error("第三方接口异常 finally",e);
                if(e.getCause()!=null&&e.getCause().toString().contains("SocketTimeoutException")){
                    throw new RuntimeException("第三方服务连接超时，请稍候重试！");
                }
                throw new RuntimeException("第三方服务异常，请稍候重试！");
            }
        }
        return str;
    }
    // 返回一维String数组
    public String[] getArray(String Option) throws AxisFault
    {
        RPCServiceClient serviceClient =this.setOption();

        opAddEntry =this.getQname(Option);

        String[] strArray = (String[]) serviceClient.invokeBlocking(opAddEntry,
                new Object[]{}, new Class[]{String[].class })[0];
        return strArray;
    }
    //从WebService中返回一个对象的实例
    public Object getObject(String Option,Object o) throws AxisFault
    {
        RPCServiceClient serviceClient =this.setOption();
        QName qname=this.getQname(Option);
        Object object = serviceClient.invokeBlocking(qname, new Object[]{},new Class[]{o.getClass()})[0];
        return object;
    }

/////////////////////////////////////////       读者可以自己封装数据类型，如int,byte,float等数据类型
}
